<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;
<<<<<<< HEAD
=======
<<<<<<< HEAD
>>>>>>> a40bd7c37eef93aca4b7ae3126fd10da65b4232b
use app\admin\model\User as UserModel;
use app\admin\model\Role as RoleModel;
use app\admin\controller\Admin;


class User extends Admin
{
    // public function index()
    // {
    //     return view('admin@user/tables');
    // }

    /**
     * 查询用户列表/所属角色
     */
    public function index()
    {
        // echo db('user')->getlastsql(); die;
        $list = db('user')->where(array('status'=>array('eq', 1)))->select();
        // var_dump($list); exit;
        $arr = array(); //声明一个空数组
        //遍历用户信息
        foreach($list as $v){
            $role_ids = db('user_role')->field('rid')->where(array('uid'=>array('eq',$v['id'])))->select();

            // var_dump($role_ids);
            //定义空数组
            $roles = array();
            //遍历
            foreach($role_ids as $value){
                // var_dump($value); exit;
                $roles[] = db('role')->where(array('id'=>array('eq',$value['rid']),'status'=>array('eq',1)))->column('name');
            }
            // var_dump($role_ids); exit;
            $v['role'] = $roles; //将新得到角色信息放置到$v中
            $arr[] = $v;
        }
        // var_dump($arr);exit;
        return view('admin@user/index', [
            'list' => $arr
<<<<<<< HEAD
=======
=======

class User extends Admin
{
    /**
     * 用户列表查询 并 渲染页面
     */
    public function userAll()
    {
        //查询数据
        $list = db('user_test')->select();
        
        $arr = array(); //声明一个空数组
        //遍历用户信息
        foreach($list as $v){
            $role_ids = db('user_role_test')->field('rid')->where(array('uid'=>array('eq',$v['id'])))->select();
            //定义空数组
            $roles = array();

            //遍历用户的角色信息
            foreach($role_ids as $value){
                $roles = db('role_test')->field(`rolename`,`department`)->where('id='.$value['rid'])->find();
            }
            if (!empty($roles)) {
                $v['role'] = $roles['rolename']; //将新得到角色信息放置到$v中
                $v['department'] = $roles['department']; //将新得到角色信息放置到$v中
            }
            $arr[] = $v;
        }
        //渲染页面
        return view('admin@user/userAll',[
            'arr'=> $arr
>>>>>>> 2042d0c0398cdc6116be32c9963026791e40783c
>>>>>>> a40bd7c37eef93aca4b7ae3126fd10da65b4232b
        ]);
    }

    /**
<<<<<<< HEAD
=======
<<<<<<< HEAD
>>>>>>> a40bd7c37eef93aca4b7ae3126fd10da65b4232b
     * 渲染添加用户模板
     */
    public function add()
    {
        return view('admin@user/add');
    }

    /**
     * 执行添加用户
     */
    public function doAdd(Request $request)
    {
        $p = input('post.');
        // dump($p); exit;
        $user = new UserModel();
        $user->data($p);
        $user->userpass = $user->setPass($p['userpass']);
        // var_dump($user);
        $result = $user->allowField(true)->save();
        //var_dump($result);
        //var_dump($user->id);
        if ($result > 0) {
            return $this->success('添加成功!', url('admin/user/index'));
        } else {
            return $this->error('添加失败!');
        }
    }

    /**
     * 渲染编辑用户页面
     */
    public function edit()
    {
        $id = input('param.');
        // var_dump($id);
        $user = db('user')->field(['id','username', 'name'])->find($id);
        // var_dump($user);exit;
        return view('admin@user/edit', [
            'user' => $user
        ]);
    }

    /**
     * 执行编辑用户
    */
    public function doEdit()
    {
        $data = input('param.');
        $id = $data['id'];
        $result = Db::table('dj_user')->where('id', $id)->update($data);
        // var_dump($result); die;
        if ($result > 0) {
            return $this->success('更新成功!', url('admin/user/index'));
        } else {
            return $this->error('更新失败!');
        }
    }


    /**
     * 渲染增加角色页面
     */
    public function showRole()
    {
        // 用户id
        $id = input('param.');

        //查询节点信息
        $list = db('role')->where('status=1')->select();
        //查询当前用户信息
        $users = db('user')->where(array('id'=>array('eq',$id['id'])))->find();

        //获取当前用户的角色信息
        $rolelist = db('user_role')->where(array('uid'=>array('eq',$id['id'])))->select();

        $myrole = array(); //定义空数组
            //对用户的角色进行重组
        foreach($rolelist as $v){
            $myrole[] = $v['rid'];
        }
        // var_dump($myrole); exit;
        // $sql = 'select u.id, r.name  from user u, role r, user_role ur where u.id = ur.uid && ur.rid = r.id  && u.id = '.$id;
        
        // $urole = Db::field('user.id,role.name')->table('user u,role r,user_role ur')->where('u.id = ur.uid && ur.rid = r.id && u.id = '.$id)->select();
        //var_dump($users,$myrole);die;
        return view('admin@user/showrole',[
            'user' => $users,
            'list' => $list,
            'role' => $myrole
        ]);
    }



    /**
     * 保存用户所属角色
     */
    public function saveRole()
    {

        $post = input('post.');
        // var_dump($post);exit;
        //判读必须选择一个角色
        if(empty($post['role'])){
            $this->error("请选择一个角色！");
        }

        $uid = $post['uid'];

        //清除用户所有的角色信息，避免重复添加
        db('user_role')->where(array('uid'=>array('eq',$uid)))->delete();

        foreach($post['role'] as $v){
            $data['uid'] = $uid;
            $data['rid'] = $v;
            //执行添加
            db('user_role')->data($data)->insert();
        }

        $this->success("角色分配成功",url('admin/user/index'));
    }



    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {   
        
        // $result = db('user')->where(array('status'=>array('eq', '2')))->update($id);
<<<<<<< HEAD
        // $sql = 'update `dj_user` set status = 2 where id =' .$id;
        // $result =Db::exec($sql);
=======
>>>>>>> a40bd7c37eef93aca4b7ae3126fd10da65b4232b
        $result = db('user')->delete($id);
        if ($result > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为'.$id.'的角色拉黑成功!';
        }else{
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = '操作失败';
        }


        // echo json_encode($result);
        return json($info);
    }


<<<<<<< HEAD
}
=======
}
=======
     * 渲染用户添加页
     */
    public function userAdd()
    {
        return view('admin@user/userAdd');
    }

    /**
     * 执行用户添加
     * @param  Request $request 
     */
    public function userDoAdd(Request $request)
    {   
        $data = $request->post();
        // var_dump($data);die;
        Db::table('user_test')->insert($data);
        $this->success('添加成功!', url('admin/user/userAll'));
    }

    /**
     * 渲染用户角色分配页面
     * @param   $id [当前用户ID]
     */
    public function userRoleList($id)
    {   
        //查询职位信息
        $list = db('role_test')->where('status=1')->select();
        //查询用户信息
        $data = db('user_test')->where('id='.$id)->find();
        // var_dump($data,$list);
        //查询当前用户角色信息
        $myrole = db('user_role_test')->field('rid')->where('uid='.$id)->find();
        
        foreach ($list as  $v) {
            if ($myrole['rid']==$v['id']) {
                $data['role'] = $v['rolename'];
                $data['department'] = $v['department'];
            }
        }
        // var_dump($list,$myrole,$data);
        // exit;
        return view('admin@user/userRoleList',[
            'data'=> $data,
            'list'=>$list,
            'role'=>$myrole
        ]);
    }

    /**
     * 执行角色分配 返回用户列表页
     */
    public function saverole(Request $request)
    {
            $data = $request->post();
            // var_dump($data);die;
            //判读必须选择一个角色
            if(empty($data['rid'])){
                $this->error("请选择一个角色！");
            }
            //清除用户所有的角色信息，避免重复添加
            db('user_role_test')->where('uid='.$data['uid'])->delete();
                //执行添加
                db('user_role_test')->insert($data);
            $this->success("角色分配成功",url('admin/user/userAll'));
        } 
}


    

>>>>>>> 2042d0c0398cdc6116be32c9963026791e40783c
>>>>>>> a40bd7c37eef93aca4b7ae3126fd10da65b4232b
